package manager;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import util.HibernateUtil;
import bean.rule.RuleCondition;
import bean.rule.RuleInformation;
import bean.rule.RuleParameter;

public class RuleManager {
	
	 Session session = null;

	public List<RuleParameter> getRuleParameters(){
		
		session = HibernateUtil.prepareSession();	
		String QUERY = "from RuleParameter rp order by rp.longName ";
		Query hql = session.createQuery(QUERY);
		List<RuleParameter> lstRuleParameter = hql.list();		
		HibernateUtil.closeSession(session);
		return lstRuleParameter;
	}
	
	public List<RuleInformation> getRules(){
		
		session = HibernateUtil.prepareSession();	
		String QUERY = "from RuleInformation ri where ri.ruleId = 2";
		Query hql = session.createQuery(QUERY);
		List<RuleInformation> lstRuleParameter = hql.list();		
		HibernateUtil.closeSession(session);
		return lstRuleParameter;
	}
	
	public String createRule(String ruleName, String ruleDescription, RuleCondition[] arrRuleConditions, String disease){
		
		session = HibernateUtil.prepareSession();
		Transaction t = null;
		Serializable savedId = null;
		try{
			
			RuleInformation rule = new RuleInformation();
			rule.setRuleName(ruleName);
			rule.setRuleDescription(ruleDescription);			
			rule.setRuleConditions(new HashSet(Arrays.asList(arrRuleConditions)));			
			rule.setDisease(disease);			
			
			//Create transaction
			HibernateUtil.startTransaction(session);
				//Save record
				savedId = session.save(rule);
				session.flush();
				//commit transaction
				HibernateUtil.commitTransaction();
			
			
		}catch(Exception ex){
			ex.printStackTrace();
			HibernateUtil.rollbackTransaction();		
		}
		finally{		
			HibernateUtil.closeSession(session);		
			return savedId.toString();
		}
		
		
		
		
	}
	
	public List<String> getDiseases(){
		
		session = HibernateUtil.prepareSession();	
		String QUERY = "select d.disease from Disease d where disease not in (select disease from RuleInformation) order by d.disease";
		Query hql = session.createQuery(QUERY);
		List<String> lstDisease = hql.list();		
		HibernateUtil.closeSession(session);
		return lstDisease;
	}
	
	
	public RuleInformation getRuleForDisease(String disease){
		session = HibernateUtil.prepareSession();
		List lstRules = session.createCriteria(RuleInformation.class).add(Restrictions.eq("disease", disease)).list();
		RuleInformation ruleInfo = (RuleInformation)lstRules.get(0);
		return ruleInfo;
	}
	public static void main(String[] args){
		System.out.println(new RuleManager().getRuleForDisease("Type 2 Disease"));
	}
	
	public List<String> getDiseasesWithRules(){
		
		session = HibernateUtil.prepareSession();	
		String QUERY = "select d.disease from Disease d where disease in (select disease from RuleInformation) order by d.disease";
		Query hql = session.createQuery(QUERY);
		List<String> lstDisease = hql.list();		
		HibernateUtil.closeSession(session);
		return lstDisease;
	}
	
	
}
